package triggerhelper.errorChecks.possibleErrors;

import java.awt.Component;
import javax.swing.JOptionPane;
import triggerhelper.errorChecks.PossibleError;

public class AlphaNumeric implements PossibleError{

    private boolean checkFirstLetter;

    public AlphaNumeric(boolean checkFirstLetter_){
        checkFirstLetter = checkFirstLetter_;
    }

    public boolean check(String input, String name, Component parent) {
        boolean valid = true;
        final char[] chars = input.toCharArray();
        for(char c : chars){
            if (!Character.isUnicodeIdentifierPart(c) && c != ' ') {
                valid = false;
                JOptionPane.showMessageDialog(parent, "I'm sorry, the " + name + " needs to be alphanumeric.");
                break;
            }
        }
        if(checkFirstLetter && !Character.isLetter(chars[0])){
            valid = false;
            JOptionPane.showMessageDialog(parent, "I'm sorry, the " + name + " needs to start with a letter.");
        }
        return valid;
    }

}
